﻿using System;
using Newtonsoft.Json;

namespace Ecobee.Net
{
	public class TokenConverter : JsonConverter
	{
		public override bool CanConvert(Type objectType)
		{
			bool returnValue = false;


			return returnValue;
		}

		public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
		{
			object returnValue = null;

			if (objectType == typeof(Token))
			{
				if (existingValue == null)
				{
					returnValue = new Token(Token.Kind.None, (string)reader.Value);
				}
				else
				{
					((Token)existingValue).Identifier = (string)reader.Value;
					returnValue = existingValue;
				}
			}

			return returnValue;
		}

		public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
		{
			writer.WriteValue(((Token)value).Identifier);
		}
	}
}
